package com.unionftech.common.webview

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.FrameLayout
import com.unionftech.common.R
import com.unionftech.common.base.BaseActivity
import com.unionftech.common.webview.callback.WebViewCallBack
import com.unionftech.common.webview.view.BaseWebView

class CommonWebViewActivity : BaseActivity(), WebViewCallBack {
    private val mFlWebContent: FrameLayout by lazy { findViewById(R.id.fl_web_content) }
    private var mTitle: String = ""
    private var mUrl: String = ""
    private var mWebView: BaseWebView? = null

    companion object {
        private const val TAG = "CommonWebViewActivity"
        private const val PARAMS_TITLE_KEY = "params_title"
        private const val PARAMS_URL_KEY = "params_url"

        @JvmStatic
        fun startPdfFile(context: Context, title: String, url: String) {
            val resultUrl = "file:///android_asset/pdf/pdfReader.html?$url"
            startThis(context, title, resultUrl)
        }

        @JvmStatic
        fun startThis(context: Context, title: String, url: String) {
            val intent = Intent(context, CommonWebViewActivity::class.java)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            intent.putExtra(PARAMS_TITLE_KEY, title)
            intent.putExtra(PARAMS_URL_KEY, url)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun initLayout() = R.layout.activity_common_web_view

    override fun obtainIntentData(intent: Intent) {
        super.obtainIntentData(intent)
        mTitle = intent.getStringExtra(PARAMS_TITLE_KEY) ?: ""
        mUrl = intent.getStringExtra(PARAMS_URL_KEY) ?: ""
    }

    override fun initData() {
        super.initData()
        mWebView = WebViewPool.getInstance().getWebView(mContext)
        mWebView?.registerWebViewCallBack(this)
    }

    override fun initView() {
        super.initView()
        val layoutParams = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT
        )
        mFlWebContent.addView(mWebView, layoutParams)
    }

    override fun setView() {
        super.setView()
        initTitleBar()
        if (!TextUtils.isEmpty(mUrl)) {
            mWebView?.visibility = View.INVISIBLE
            mWebView?.loadUrl(mUrl)
        }
    }

    private fun initTitleBar() {
        if (!TextUtils.isEmpty(mTitle)) {
            setActivityTitle(mTitle)
            mRootView.setTitleColor(mContext.getColor(R.color.color_1C1D21))
        }
    }

    override fun pageStarted(url: String) {

    }

    override fun pageFinished(url: String) {
        mWebView?.visibility = View.VISIBLE
    }

    override fun overrideUrlLoading(url: String): Boolean {
        return false
    }

    override fun onError(errorCode: Int, description: String, failingUrl: String) {

    }

    override fun onDestroy() {
        mFlWebContent.removeAllViews()
        mWebView?.let {
            it.registerWebViewCallBack(null)
            WebViewPool.getInstance().recycle(it)
        }
        mWebView = null
        super.onDestroy()
    }

}